package com.it.service;

import com.it.mapper.ClShoppingCarMapper;
import com.it.mapper.LinkFoodCartMapper;
import com.it.pojo.ClShoppingCar;
import com.it.pojo.LinkFoodCart;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ClShoppingCarServiceImpl implements ClShoppingCarService {

	//由spring创建对象并为变量赋值
	@Autowired
	private ClShoppingCarMapper clShoppingCarMapper;

	@Autowired
	private LinkFoodCartMapper linkFoodCartMapper;

	/**
	 * 1.查询所有购物车信息
	 * @return
	 */
	public List<ClShoppingCar> findAll(){
		//1.调用shoppingCarMapper的findAll方法查询所有购物车
		List<ClShoppingCar> list = clShoppingCarMapper.findAll();
		for (ClShoppingCar cart:list){
			List<LinkFoodCart> links = linkFoodCartMapper.findByCartId(cart.getId());
			cart.setLinkFoodCarts(links);
		}
		return list;
	}
	
	/**
	 * 2.根据id删除购物车信息
	 * @param id
	 */
	public int deleteById(String id){
		//1.调用ShoppingCarMapper的deleteById方法
		//根据id删除购物车信息
		int result = clShoppingCarMapper.deleteById(id);
		return result;
	}
	
	/**
	 * 3.添加购物车信息
	 * @param cart
	 */
	public int addClShoppingCar(ClShoppingCar cart){
		int result = clShoppingCarMapper.addClShoppingCar(cart);
		return result;
	}
	
	/**
	 * 4.1.根据id查询购物车信息
	 * @param id
	 * @return 
	 */
	public ClShoppingCar findById(String id){
		ClShoppingCar cart = clShoppingCarMapper.findById(id);
		List<LinkFoodCart> links = linkFoodCartMapper.findByCartId(cart.getId());
		cart.setLinkFoodCarts(links);
		return cart;
	}

	/**
	 * 4.2.根据id查询购物车信息
	 * @param userId
	 * @return
	 */
	public List<ClShoppingCar> findByUserId(String userId) {
		List<ClShoppingCar> list = clShoppingCarMapper.findByUserId(userId);
		for (ClShoppingCar cart:list){
			List<LinkFoodCart> links = linkFoodCartMapper.findByCartId(cart.getId());
			cart.setLinkFoodCarts(links);
		}
		return list;
	}

	/**
	 * 4.3.根据id查询已经下单的购物车信息
	 * @param userId
	 * @return
	 */
	public List<ClShoppingCar> findHistoryByUserId(String userId) {
		List<ClShoppingCar> list = clShoppingCarMapper.findHistoryByUserId(userId);
		for (ClShoppingCar cart:list){
			List<LinkFoodCart> links = linkFoodCartMapper.findByCartId(cart.getId());
			cart.setLinkFoodCarts(links);
		}
		return list;
	}

	/**
	 * 5.根据id修改购物车信息
	 * @param cart
	 */
	public int updateById(ClShoppingCar cart){
		int result = clShoppingCarMapper.updateById(cart);
		return result;
	}
	
}
